package com.bw.yk09;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class FlinkDWSSinkClickhouse extends RichSinkFunction<Order_info_stats> {
    Connection conn = null;
    PreparedStatement ps = null;
    String sql=null;

    public FlinkDWSSinkClickhouse(String sql)
    {
        this.sql=sql;
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
        conn = DriverManager.getConnection("jdbc:clickhouse://hadoop-single:8123/default", "default", "");
//        conn.setAutoCommit(true);
        ps = conn.prepareStatement(sql);
    }

    @Override
    public void close() throws Exception {
        ps.close();
        conn.close();
    }

    @Override
    public void invoke(Order_info_stats value, Context context) throws Exception {
        ps.setString(1,value.getStt());
        ps.setString(2,value.getEdt());
        ps.setString(3,value.getCreate_time_by_day());
        ps.setLong(4,value.getOrder_order_num());
        ps.setBigDecimal(5,value.getOrder_amount());
        ps.setLong(6,value.getTs());
        ps.execute();
    }
}
